<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"
         import="java.sql.*"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
    String User = request.getParameter("username");     //得到登录用户名
    String Pwd = request.getParameter("password");       //得到登录密码
    Connection conn = null;
    Statement stmt = null;
    String url = "jdbc:mysql://localhost:3306/newsdb?useUnicode=true&characterEncoding=utf-8";
//1加载JDBC驱动      加
    try {
        Class.forName("com.mysql.jdbc.Driver");
//2建立与数据库的连接    连
        conn = DriverManager.getConnection(url, "root", "root");

//3创建操作数据库SQL的Statement对象      预
        String sql = "insert into users (username,password) values ('"+User+"','"+Pwd+"')";
        stmt = conn.createStatement();
//4执行SQL语句，并分析执行结果             执
        int num = stmt.executeUpdate(sql);
        if (num>0)
        { //插入记录成功
            out.println("注册成功");
         }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
//5关闭连接，释放资源       释
        if (stmt!=null){
            try {
                stmt.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            stmt=null;
        }
        if (conn!=null){
            try {
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            conn=null;
        }
    }
%>
</body>
</html>
